package dmf.generator.ecore2d.popup.actions;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class GenerateDAction implements IObjectActionDelegate {

	private Shell shell;
	IResource resource;
	
	/**
	 * Constructor for Action1.
	 */
	public GenerateDAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		System.out.println(resource.getFileExtension());
		if (resource.getFileExtension().equals("ecore"))
			Generator.generateNative(resource.getLocation(), resource.getProject().getLocation());
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		TreeSelection sel = ((TreeSelection)selection);
		resource = (IResource)sel.getFirstElement();
	}

}
